import {defineStore} from "pinia";
import {getRouters} from "@/api/login";
import {constantRoutes} from "@/router";
import {CommonConstant} from "@/common/constant";
import Layout from '@/layout/index.vue';
import ParentView from '@/components/ParentView/index.vue';

// 匹配views里面所有的.vue文件
const modules: any = import.meta.glob('@/views/**/**.vue');

const routes = defineStore('routes', {
    state: () => ({
        routes: [] as any,
        addRoutes: [] as any,
    }),
    actions: {
        //追加路由信息
        setRoutes(routes: any) {
            this.addRoutes = routes;
            this.routes = constantRoutes.concat(routes);
        },
        //生成路由
        GenerateRoutes() {
            return new Promise((resolve) => {
                getRouters().then(res => {
                    const sdata = JSON.parse(JSON.stringify(res.data))
                    const rdata = JSON.parse(JSON.stringify(res.data))
                    const sidebarRoutes = filterAsyncRouter(sdata)
                    const rewriteRoutes = filterAsyncRouter(rdata, true)
                    this.setRoutes(sidebarRoutes)
                    resolve(rewriteRoutes)
                })
            })
        }
    }
})

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap: any, type: boolean = false) {
    return asyncRouterMap.filter((route: any) => {
        if (type && route.children) {
            route.children = filterChildren(route.children)
        }
        if (route.component) {
            // Layout ParentView 组件特殊处理
            if (route.component === CommonConstant.LAYOUT) {
                route.component = Layout
            } else if (route.component === CommonConstant.PARENT_VIEW) {
                route.component = ParentView
            } else {
                route.component = loadView(route.component)
            }
        }
        if (route.children != null && route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children, type)
        } else {
            delete route['children']
            delete route['redirect']
        }
        return true
    })
}

function filterChildren(childrenMap: any, lastRouter: any = false) {
    let children: any[] = []
    childrenMap.forEach((el: any) => {
        if (el.children && el.children.length) {
            if (el.component === 'ParentView' && !lastRouter) {
                el.children.forEach((c: any) => {
                    c.path = el.path + '/' + c.path
                    if (c.children && c.children.length) {
                        children = children.concat(filterChildren(c.children, c))
                        return
                    }
                    children.push(c)
                })
                return
            }
        }
        if (lastRouter) {
            el.path = lastRouter.path + '/' + el.path
        }
        children = children.concat(el)
    })
    return children
}

export const loadView = (view: any) => {
    //modules模块信息 /src/views/home.vue :  () => import("/src/views/home.vue")
    //根据返回的路由component拼接模块字符串，匹配对应路由信息
    return modules[`/src/views/${view}.vue`];
}

export default routes